﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Linq;
using Manager.App_Code;
using Manager.Common;
using Manager.ViewModels;

namespace Manager.FormMain
{
    public partial class Frm_BarButton_DanhSachPhongCheckout : DevExpress.XtraEditors.XtraForm
    {
        NewPearl_HotelDataContext db = new NewPearl_HotelDataContext();
        List<ThongKeCheckOut> lstThongKeCheckOut = null;
        double tongThanhTienVND_CheckOut = 0;
        double tongThanhTienUSD_CheckOut = 0;
        double tongThanhTienVND_TienCoc = 0;
        double tongThanhTienUSD_TienCoc = 0;

        public Frm_BarButton_DanhSachPhongCheckout()
        {
            InitializeComponent();
        }        

        private void Frm_BarButton_DanhSachPhongCheckout_Load(object sender, EventArgs e)
        {            
            lstThongKeCheckOut = new List<ThongKeCheckOut>();
            dateTuNgay.DateTime = DateTime.Now;
            dateDenNgay.DateTime = DateTime.Now;

            SearchCheckOut(dateTuNgay.DateTime, dateDenNgay.DateTime);
        }

        private void btnFind_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(dateTuNgay.Text) || string.IsNullOrEmpty(dateDenNgay.Text))
            {
                MessageBox.Show("Vui lòng chọn từ ngày - đến ngày!");
                return;
            }
            if (dateTuNgay.DateTime > dateDenNgay.DateTime)
            {
                MessageBox.Show("Từ ngày không được lớn hơn đến ngày!");
                return;
            }
            SearchCheckOut(dateTuNgay.DateTime, dateDenNgay.DateTime);
        }

        private void SearchCheckOut(DateTime tuNgay, DateTime denNgay)
        {
            try
            {
                if (lstThongKeCheckOut.Count > 0)
                    lstThongKeCheckOut.Clear();
                tongThanhTienVND_CheckOut = 0;
                tongThanhTienUSD_CheckOut = 0;
                tongThanhTienVND_TienCoc = 0;
                tongThanhTienUSD_TienCoc = 0;
                txtTongTienVND_CheckOut.Text = "";
                var lstChiTietCheckOut = db.ChiTiet_CheckOuts.Where(t => tuNgay.Date <= t.NgayDi.Value.Date && t.NgayDi.Value.Date <= denNgay.Date)
                                                             .OrderBy(t => t.NgayDi).ToList();
                #region check out
                if (lstChiTietCheckOut.Count() > 0)
                {
                    for (int i = 0; i < lstChiTietCheckOut.Count; i++)
                    {
                        var chiTiet = lstChiTietCheckOut[i];
                        var thongKeCheckOut = new ThongKeCheckOut();
                        thongKeCheckOut.dateNgayTra = chiTiet.NgayDi.Value;
                        thongKeCheckOut.strNgayTra = thongKeCheckOut.dateNgayTra.ToString("dd/MM/yyyy");
                        thongKeCheckOut.dateNgayDen = chiTiet.NgayDen.Value;
                        thongKeCheckOut.strNgayDen = thongKeCheckOut.dateNgayDen.ToString("dd/MM/yyyy");
                        thongKeCheckOut.MaPhong = chiTiet.CheckOut.MaPhong;
                        thongKeCheckOut.MaPhieuDangKy = chiTiet.CheckOut.MaPhieuDangKy;
                        thongKeCheckOut.GiaPhong = chiTiet.GiaTien.Value;
                        thongKeCheckOut.strGiaPhong = CommonFunction.AddSeparator(thongKeCheckOut.GiaPhong);
                        thongKeCheckOut.TienTe = chiTiet.MaTienTe;
                        thongKeCheckOut.TienDichVuVND = 0;
                        thongKeCheckOut.TongTienVND = 0;
                        thongKeCheckOut.Loai = "Check out";
                        
                        List<PhuThuDichVu> lstPhuThuDichVu = null;
                        if (thongKeCheckOut.dateNgayTra > thongKeCheckOut.dateNgayDen)
                        {
                            if (i == 0)
                                lstPhuThuDichVu = db.PhuThuDichVus.Where(t => t.MaPhieuDangKy == thongKeCheckOut.MaPhieuDangKy
                                                                    && (thongKeCheckOut.dateNgayDen.Date == t.NgayTao.Value.Date || t.NgayTao.Value.Date == thongKeCheckOut.dateNgayTra.Date)).ToList();
                            else
                                lstPhuThuDichVu = db.PhuThuDichVus.Where(t => t.MaPhieuDangKy == thongKeCheckOut.MaPhieuDangKy
                                                                    && t.NgayTao.Value.Date == thongKeCheckOut.dateNgayTra.Date).ToList();
                        }
                        else
                            lstPhuThuDichVu = db.PhuThuDichVus.Where(t => t.MaPhieuDangKy == thongKeCheckOut.MaPhieuDangKy).ToList();
                        if (lstPhuThuDichVu.Count > 0)
                        {
                            foreach (var phuThu in lstPhuThuDichVu)
                            {
                                if (phuThu.GiaTien == null || phuThu.SoLuong == null)
                                    continue;
                                double thanhTienVND = 0;
                                double thanhTienUSD = 0;
                                var thanhTien = phuThu.GiaTien.Value * phuThu.SoLuong.Value;
                                if (phuThu.MaTienTe == "USD")
                                    thanhTienUSD = thanhTien;
                                else if (phuThu.MaTienTe == "VND")
                                    thanhTienVND = thanhTien;
                                thongKeCheckOut.TienDichVuVND += thanhTienVND;
                                thongKeCheckOut.TienDichVuUSD += thanhTienUSD;
                            }
                        }

                        thongKeCheckOut.strTienDichVuVND = CommonFunction.AddSeparator(thongKeCheckOut.TienDichVuVND);
                        thongKeCheckOut.strTienDichVuUSD = CommonFunction.AddSeparator(thongKeCheckOut.TienDichVuUSD);
                        if (chiTiet.MaTienTe == "VND")
                        {
                            thongKeCheckOut.TienDichVuVND += thongKeCheckOut.GiaPhong;
                            thongKeCheckOut.strTongTienVND = CommonFunction.AddSeparator(thongKeCheckOut.TienDichVuVND);
                        }
                        else if (chiTiet.MaTienTe == "USD")
                        {
                            thongKeCheckOut.TienDichVuUSD += thongKeCheckOut.GiaPhong;
                            thongKeCheckOut.strTongTienUSD = CommonFunction.AddSeparator(thongKeCheckOut.TienDichVuUSD);
                        }

                        lstThongKeCheckOut.Add(thongKeCheckOut);
                        tongThanhTienVND_CheckOut += thongKeCheckOut.TienDichVuVND;
                        tongThanhTienUSD_CheckOut += thongKeCheckOut.TienDichVuUSD;
                    }
                    txtTongTienVND_CheckOut.Text = CommonFunction.AddSeparator(tongThanhTienVND_CheckOut);
                    txtTongTienUSD_CheckOut.Text = CommonFunction.AddSeparator(tongThanhTienUSD_CheckOut);
                }
                #endregion

                #region dat phong
                var lstPhieuDatPhong = db.PhieuDatPhongs.Where(t => tuNgay.Date <= t.Ngaylap.Value.Date && t.Ngaylap.Value.Date <= denNgay.Date);
                if (lstPhieuDatPhong.Count() > 0)
                {
                    foreach (var phieuDatPhong in lstPhieuDatPhong)
                    {
                        var thongKeCheckOut = new ThongKeCheckOut();
                        thongKeCheckOut.dateNgayTra = phieuDatPhong.Ngaylap.Value;
                        thongKeCheckOut.strNgayTra = thongKeCheckOut.dateNgayTra.ToString("dd/MM/yyyy");
                        thongKeCheckOut.dateNgayDen = phieuDatPhong.NgayDen.Value;
                        thongKeCheckOut.strNgayDen = thongKeCheckOut.dateNgayDen.ToString("dd/MM/yyyy");
                        thongKeCheckOut.MaPhong = "Đặt phòng từ ngày: " + phieuDatPhong.NgayDen.Value.ToString("dd/MM/yyyy") + " đến ngày: " + phieuDatPhong.NgayDi.Value.ToString("dd/MM/yyyy");
                        thongKeCheckOut.MaPhieuDangKy = CommonFunction.ParseLong(phieuDatPhong.MaPhieuDat);
                        thongKeCheckOut.GiaPhong = 0;
                        thongKeCheckOut.strGiaPhong = "0";
                        thongKeCheckOut.TienTe = "USD";
                        thongKeCheckOut.TienDichVuVND = 0;
                        thongKeCheckOut.TongTienVND = 0;
                        thongKeCheckOut.strTongTienVND = "0";
                        thongKeCheckOut.TongTienUSD = 0;
                        thongKeCheckOut.strTongTienUSD = "0";
                        thongKeCheckOut.Loai = "DatPhong";

                        if (phieuDatPhong.DonViTienTe == "VND")
                        {
                            tongThanhTienVND_TienCoc += phieuDatPhong.TienCoc.Value;
                        }
                        else if (phieuDatPhong.DonViTienTe == "USD")
                            tongThanhTienUSD_TienCoc += phieuDatPhong.TienCoc.Value;

                        lstThongKeCheckOut.Add(thongKeCheckOut);
                    }                    
                    txtTongTienVND_TienCoc.Text = CommonFunction.AddSeparator(tongThanhTienVND_TienCoc);
                    txtTongTienUSD_TienCoc.Text = CommonFunction.AddSeparator(tongThanhTienUSD_TienCoc);
                }
                #endregion
                grid_CheckOut.DataSource = null;
                grid_CheckOut.DataSource = lstThongKeCheckOut;
            }
            catch (Exception ex)
            {
                Log.WriteLog("SearchCheckOut: " + ex.Message + "\n" + ex.StackTrace);
            }
        }

        private bool CheckLastNgayTra(long? CheckOutId, DateTime ngayTra)
        {
            var flag = false;
            var lstChiTietCheckOut = db.ChiTiet_CheckOuts.Where(t => t.CheckOutId == CheckOutId);
            if (lstChiTietCheckOut.Count() > 0)
            {
                var date = lstChiTietCheckOut.Max(t => t.NgayDi);
                if (date.Value == ngayTra)
                    flag = true;
            }
            return flag;
        }

        private void cmbTienTe_SelectedIndexChanged(object sender, EventArgs e)
        {
            //var tienTe = cmbTienTe.SelectedItem as TienTe;
            //var tongTien = Math.Round((tongThanhTienVND / tienTe.TyGiaThamKhao.Value), 2);
            //txtTongTienVND.Text = CommonFunction.AddSeparator(tongTien);
        }
    }
}